package com.bluedot.bank.action;

import com.bluedot.bank.dao.Pagination;
import com.bluedot.bank.dao.customer.CustomerConditons;
import com.bluedot.bank.dao.user.User;
import com.bluedot.bank.dao.user.UserDao;
import com.bluedot.bank.dao.user.UserDaoImpl;
import com.opensymphony.xwork2.ActionSupport;

public class FindUserAction extends ActionSupport {
	private Pagination<User> pagination=new Pagination<User>();
	private int filterName;
	private String value_u;
	@Override
	public String execute() throws Exception {
		UserDao userDao=new UserDaoImpl();
		switch (filterName) {
		case 1:
			pagesResult(pagination, null);
			userDao.browse(pagination);
			break;
		case 2:
			pagesResult(pagination, getCondition("userIdFilter", "userId",
					Long.parseLong(value_u)));
			userDao.browse(pagination);
			break;
		case 3:
			pagesResult(pagination, getCondition("userNameFilter",
					"userName", "%" + value_u + "%"));
			userDao.browse(pagination);
			break;
		
		default:
			pagesResult(pagination, null);
			userDao.browse(pagination);
			break;
		}
		return SUCCESS;
	}

	void pagesResult(Pagination<User> pagination,
			CustomerConditons conditons) {
		if (this.pagination == null) {
			this.pagination = new Pagination<User>();
		}

		if (this.pagination.getSize() == 0) {
			this.pagination.setSize(3);
		}

		if (this.pagination.getNo() == 0) {
			this.pagination.setNo(1);
		}
		pagination.setConditon(conditons);
	}

	CustomerConditons getCondition(String filterName, String key, Object o) {
		CustomerConditons conditons = new CustomerConditons();
		conditons.setFilterName(filterName);
		conditons.setKey(key);
		conditons.setO(o);
		return conditons;
	}

	/**
	 * @return the pagination
	 */
	public Pagination<User> getPagination() {
		return pagination;
	}

	/**
	 * @param pagination the pagination to set
	 */
	public void setPagination(Pagination<User> pagination) {
		this.pagination = pagination;
	}

	/**
	 * @return the filterName
	 */
	public int getFilterName() {
		return filterName;
	}

	/**
	 * @param filterName the filterName to set
	 */
	public void setFilterName(int filterName) {
		this.filterName = filterName;
	}

	/**
	 * @return the value_u
	 */
	public String getValue_u() {
		return value_u;
	}

	/**
	 * @param value_u the value_u to set
	 */
	public void setValue_u(String value_u) {
		this.value_u = value_u;
	}

}
